<script type="text/javascript">

function checkvalid(id)
{
		var error_flag = 0;
		
		$("#error_discount_offer"+id).hide();
		$("#error_valid_from"+id).hide();
		$("#error_valid_to"+id).hide();	
		$("#error_min_stay"+id).hide();	
		$("#error_bookby"+id).hide();	

	if(isNaN($('#discount_offer'+id).val()) || $('#discount_offer'+id).val() == "" || $('#discount_offer'+id).val() == null || $("#discount_offer"+id).val().length >3)
	{
		$("#error_discount_offer"+id).show();
		error_flag = 1;
	} 
	
	if(/Invalid|NaN/.test(new Date($("#valid_from"+id).val())) || $("#valid_from"+id).val() == "" || $("#valid_from"+id).val() == null)
	{
		$("#error_valid_from"+id).show();	
		error_flag = 1;		
	}
	
		if(/Invalid|NaN/.test(new Date($("#valid_to"+id).val())) || $("#valid_to"+id).val() == "" || $("#valid_to"+id).val() == null )
	{
		$("#error_valid_to"+id).show();	
		error_flag = 1;		
	}
	
	if(isNaN($("#min_stay"+id).val()) || $("#min_stay"+id).val() == "" || $("#min_stay"+id).val() == null ||  $("#min_stay"+id).val().length >3)
	{
		$("#error_min_stay"+id).show();	
		error_flag = 1;		
	}
	
	if(/Invalid|NaN/.test(new Date($("#bookby"+id).val())) || $("#bookby"+id).val() == "" || $("#bookby"+id).val() == null)
	{
		$("#error_bookby"+id).show();	
		error_flag = 1;		
	}	
	

	
	if(error_flag == 0)
	$.ajax({
	
			url: "<?=APPLICATION_URL?>myaccount/saveoffers",
			type:"POST",
			data: ({Valid_f:$("#valid_from"+id).val(), Valid_t:$("#valid_to"+id).val(), Nights:$("#min_stay"+id).val(), Discount:$("#discount_offer"+id).val(), Book_by: $("#bookby"+id).val() ,Offer_id:$("#offer_id"+id).val()}),
			async:false,
			success: function(data){
				data = $.trim(data);	
				window.location = window.location;
			}
		
		
	})
}

function dactive(id)
{
	$.ajax({
	
			url: "<?=APPLICATION_URL?>myaccount/deactivateoffers/id/"+$("#spcl_offer_id"+id).val(),
			type:"GET",
			async:false,
			success: function(data){
				data = $.trim(data);	
				window.location.href = "<?=APPLICATION_URL?>myaccount/rental";
			}
		
		
	})
	
}

$(document).ready(function() {


		  do_ppty(); //calling function for getting rates
		  extras_show();			
		//**** Following is the date code *****//
		//****        for rental rate     *****//	

				 var minday = $("#cal_date").val();
			
				 if(minday != "")
				 {
					 var DateF = minday.split("-");
					 var x = DateF[0]+"/"+DateF[1]+"/"+(parseInt(DateF[2])+1);
					 var minday = new Date(x); //values for date_from
				 }
				else
				minday = new Date();  //value for date_from
				  
			
			
				  
				   var today = new Date();
				   var d = today.getDate();
				   var m = today.getMonth();
				   var y = today.getFullYear();
			
					$("#date_from").datepicker({
						minDate: minday,
						maxDate: new Date(y+2, m ,d)
						 
					});
					
					
					//operation for the second one
					
				
					$("#date_to").datepicker({
						minDate: new Date()
					});
					
					
					
				jQuery.validator.addMethod("dateCheck", function(value, element) { 
				return ($("#date_to").val() > $("#date_from").val()); 
			  }, "Choose Proper Date");
	
		//**** Above is the date code *****//
		//****  for rental rate ends  *****//	

		

		for(var i =1;i<=9;i++)
		{
			$("#valid_from"+i).datepicker({
			minDate: new Date()

			});	
			
			$("#valid_to"+i).datepicker({
		
			});
			
			$("#bookby"+i).datepicker({
		
			});
		
		}
	
});

function mindate(value,id)
{

				var selectedDate = new Date(value);
				
				var msecsInADay = 86400000;
				var endDate = new Date(selectedDate.getTime()+msecsInADay);

				$("#"+id).datepicker( "option", "minDate", endDate );
				

				
}
function mindateforbook(value,id)
{
	
				var selectedDate = new Date(value);
				
				var msecsInADay = 86400000;
				var endDate = new Date(selectedDate.getTime()-msecsInADay);

				$("#"+id).datepicker( "option", "maxDate", endDate );
}

function do_save_ppty() //function for saving rental rates
{
	
	if($("#mws-validate").valid())
	{
		var Data = 0;
		$('#rate_blck').html("<div align = 'center' style='width:100%;'><img src = '<?=IMAGES_URL?>black-001-loading.gif' width = '20' height = '20'></div>");
		
		$.ajax({
			
			url: "<?=APPLICATION_URL?>myaccount/setrates",
			type:"POST",
			data: ({Date_f:$("#date_from").val(), Date_t:$("#date_to").val(), Nights:$("#nights").val(), Rate:$("#rate").val() }),
			async:false,
			success: function(data){
	
				data = $.trim(data);
				

				//do_ppty();
				
			}
			
		})
		
		return false;	
	}
}

//function for deleting rates data

function deleterate(id) //function for deleting rental rates
{
	var flag = 0;
	$.ajax({
		
			url: "<?=APPLICATION_URL?>myaccount/deleterentalrate/id/"+id,
			type:"GET",
			async:false,
			success: function(data){
				data = $.trim(data);
				if(data != "")
				{
					alert("Record Sucessfully deleted");
					flag = 1;
				}
			}
		})
		
		if(flag == 1)
		do_ppty();
}

function do_ppty() //function for displaying rental rates
{
	var Data = 0;
	$('#rate_blck').html("<div align = 'center' style= 'clear:both;width:100%;'><img src = '<?=IMAGES_URL?>black-001-loading.gif' width = '20' height = '20'></div>");
	
	$.ajax({
		
		url: "<?=APPLICATION_URL?>myaccount/getrates",
		type:"GET",
		async:false,
		success: function(data){

			data = $.trim(data);
			$("#rate_blck").text("");

			if(data != '0' && data != "" && data != null)
			{
				var ArrData = data.split("|");
				if(ArrData.length>0)
				{
					for(var i=0;i<ArrData.length;i++)
					{
						var mySplit=ArrData[i].split(",");
						var OptionValue1 = mySplit[0];
						var OptionValue2 = mySplit[1];
						var OptionValue3 = mySplit[2];
						var OptionValue4 = mySplit[3];						
						var OptionValue5 = mySplit[4];						
						$("#rate_blck").append("<div style='width:100%;float:left;'><div style='width:20%' align = 'center'><label>"+OptionValue1+"</label></div><div style='width:20%;' align = 'center'><label >"+OptionValue2+"</label></div><div style='width:30%;' align='center'><label>"+OptionValue3+"</label></div><div style='width:25%;' align='center'><label>"+OptionValue4+"</label></div><a href = '#' style = 'display:block;'><div onclick = 'deleterate("+OptionValue5+");' ><img class='deletes' src='<?=IMAGES_URL?>TRASH - EMPTY.png' width = '20' height = '20'></div></a></div");
						
						if(i+1 == ArrData.length)
						$("#cal_date").val(OptionValue2);
					
					}
			
					
				}	
				
				
			}
			else
			{
				$("#rate_blck").html("<div align='center' style = 'color:red;width:100%;'>No Items to display</div>");
				$("#cal_date").val("");	
			}
		}
		
	})
	
	return false;	
}

function extras_show() //function for showing extras
{
	var Data = 0;
	$('#extra_blck').html("<div align = 'center' style= 'clear:both;width:100%;'><img src = '<?=IMAGES_URL?>black-001-loading.gif' width = '20' height = '20'></div>");
	$.ajax({
		
		url: "<?=APPLICATION_URL?>myaccount/getextras",
		type:"GET",
		async:false,
		success: function(data){

			data = $.trim(data);
			$("#extra_blck").text("");

			if(data != '0' && data != "" && data != null)
			{
				var ArrData = data.split("|");
				if(ArrData.length>0)
				{
					for(var i=0;i<ArrData.length;i++)
					{
						var mySplit=ArrData[i].split(",");
						var OptionValue1 = mySplit[0];
						var OptionValue2 = mySplit[1];
						var OptionValue3 = mySplit[2];
						var OptionValue4 = mySplit[3];	
						
						if(OptionValue3 == 1)					
						OptionValue3 = "Compulsory";
						else
						OptionValue3 = "Optional";
						
						$("#extra_blck").append("<div class ='inExtra' style='width:100%;float:left;'><div style='width:40%' align = 'center'><label>"+OptionValue1+"</label></div><div style='width:30%;' align = 'center'><label >"+OptionValue2+"</label></div><div style='width:20%;' align='center'><label>"+OptionValue3+"</label></div><a href = '#' style = 'display:block;'><div onclick = 'deleteextras("+OptionValue4+");' ><img class='deletes' src='<?=IMAGES_URL?>TRASH - EMPTY.png' width = '20' height = '20'></div></a></div");
						
					}
			
					
				}	
				
				
			}
			else
			{
				$("#extra_blck").html("<div align='center' style = 'color:red;width:100%;'>No Items to display</div>");

			}
		}
		
	})
	
	return false;	
	
}

function do_save_extra() //function for saving extras
{
	
		var Data = 0;
		$('#extra_blck').html("<div align = 'center' style='width:100%;'><img src = '<?=IMAGES_URL?>black-001-loading.gif' width = '20' height = '20'></div>");
		
		$.ajax({
			
			url: "<?=APPLICATION_URL?>myaccount/saveextras",
			type:"POST",
			data: ({extra_name:$("#extra_name").val(), extra_price:$("#extra_price").val(), extra_type:$("#extra_type").val()}),
			async:false,
			success: function(data){
	
				data = $.trim(data);
				//do_ppty();
			}
			
		})
		
		return false;	
	
}

function deleteextras(id) //function for deleting extras
{
	alert(id);
	var flag = 0;
	$.ajax({
		
			url: "<?=APPLICATION_URL?>myaccount/deleteextras/id/"+id,
			type:"GET",
			async:false,
			success: function(data){
				data = $.trim(data);
				if(data != "")
				{
					alert("Record Sucessfully deleted");
					flag = 1;
				}
			}
		})
		
		if(flag == 1)
		extras_show();
}

</script>

<!-- step 7 starts -->
<div style="height:auto;"><!-- rental rate wrapper div-->
<form name="myform" id="mws-validate" method="post"  novalidate="novalidate" class="mws-form" >	
	<h5>Property Daily Rental Rates Panel:</h5>
        
	    						<div class="mws-form-col-2-8 alpha">
                                    <label>Date From:</label>
                                    <div class="mws-form-item large">
                                        <input type="text" name="date_from" id = "date_from" class="mws-textinput required date" >
                                    </div>
                                </div>
                                <div class="mws-form-col-2-8">
                                    <label>Date To:</label>
                                    <div class="mws-form-item large">
                                        <input type="text" name="date_to" id = "date_to" class="mws-textinput required date dateCheck" >
                                    </div>
                                </div>
                                <div class="mws-form-col-2-8">
                                    <label>Minimum Night Stay:</label>
                                    <div class="mws-form-item large">
                                        <select name="nights" id ="nights" class="mws-textinput required" >
	                                        <option value="">- - Select - -</option>
                                        <?	
											for($i=7;$i<=14;$i++)
											{		
												if($i == 7)
												$selected = "selected = 'selected'";
												else
												$selected = "";
                                            	echo "<option '".$selected."' value='".$i."'>".$i."</option>";
											}
										?>
                                        </select>
                                    </div>
                                </div>
                                <div class="mws-form-col-2-8 omega">
                                    <label>Daily Rate</label>
                                    <div class="mws-form-item large">
                                        <input class="mws-textinput required number" name="rate"  id = "rate" type="text">
                                    </div>
                                </div>    
    	
								<div class="mws-button-row1" style="border:none;">
	        						<input type="submit" value="Update" class="mws-tooltip-n mws-button blue" original-title="Update" onClick="do_save_ppty()">
                                </div>
    </form>
     <input type="hidden" name="cal_date" id = "cal_date">
    <div style='margin-bottom:55px;'>
        <div class="rental_rate_blck">
            <label title="from">From</label>
            <label>To</label>
            <label>Minimum Night stay</label>
            <label>Daily Rate</label>
        </div>
    
        <div class="rental_rate_blck2" id = "rate_blck" style = 'display:inline-block;'>
            
        
        </div>
       
	</div>

<!-- extras panel starts -->
<h5>Extras:</h5>
<form name="myform" id="mws-validate" method="post"  novalidate="novalidate" class="mws-form" >	
	<div class="mws-form-col-2-8 alpha">
        <label>Name Of Extras:</label>
        <div class="mws-form-item large">
            <input type="text" name="extra_name" id = "extra_name" class="mws-textinput required" minLength = "50">
        </div>
    </div>
    <div class="mws-form-col-2-8">
        <label>Price:</label>
        <div class="mws-form-item large">
            <input type="text" name="extra_price" id = "extra_price" class="mws-textinput required number" >
        </div>
    </div>
    <div class="mws-form-col-2-8">
        <label>Type:</label>
        <div class="mws-form-item large">
            <select name="extra_type" id ="extra_type" class="mws-textinput required" >
                <option value="">- - Select - -</option>
                <option value="0">Optional</option>
                <option value="1">Compulsory</option>
            </select>
        </div>
    </div>
    <br />

    <div class="mws-button-row1" style="border:none;">
	      <input type="submit" value="Update" class="mws-tooltip-n mws-button blue" original-title="Update" onClick="do_save_extra()">
    </div>
	</form>    
    
    <div style="margin-bottom:55px;"><!-- display block of extras-->
        <div class="extras_blck">
            <label title="from">Name Of Extras</label>
            <label>Price</label>
            <label>Type</label>
        </div>

        <div class="extras_blck2" id = "extra_blck" style = 'display:inline-block;'>
            
        
        </div>
       
	</div>

    
    
<!-- extras panel ends -->


<!-- Special Offers tab starts -->
<h5>Special offers:</h5>



    <table class="mws-datatable-fn mws-table">
            <thead>
                <tr>
                    <th width="10%">Special Offer Type</th>
                    <th width="10%">Promo Code</th>
                    <th width="10%">Valid From</th>
                    <th width="10%">Valid Until</th>                    
                    <th width="10%">Minimum Night Stay</th>                    
                    <th width="10%">Discount Offer</th>                    
                    <th width="10%">Book By Date</th>
                    <th width="10%">Activate</th>                                                        
                </tr>
            </thead>
            <tbody>
                    
    
         <?	$i=1;

		    	foreach($this->offerArr as $values)
				{
					
					if($values['discount_type']==0){ $text = '';  }
                    if($values['discount_type']==1){ $text =' Nights Free';  } 
					
					if($values['activate'] != '1')
					{
						$disabled_d = 'disabled="disabled"';
						$disabled_a = "";
					}
					else
					{
						$disabled = "";
						$disabled_a = "disabled='disabled'";
					}
           ?>
           	    
           			<tr class="gradeX"> 
                        <td><?=$values['type_name']?></td>
	                    <td id = 'promo<?=$i?>'><?=$values['promo_code']?></td>
	                    <td><input type="text" name="valid_from<?=$i?>" id="valid_from<?=$i?>" onchange="mindate(this.value,'valid_to<?=$i?>')" value = "<? if($values['valid_from']) echo date('m/d/Y', strtotime($values['valid_from']));?>"/>
                        	<label id = 'error_valid_from<?=$i?>' for="valid_from<?=$i?>" class = "error" style="display:none;">please choose date</label>
                        </td>
	                    <td><input type="text" name="valid_to<?=$i?>" id="valid_to<?=$i?>" onchange="mindateforbook(this.value,'bookby<?=$i?>')" value="<? if($values['valid_to']) echo date('m/d/Y', strtotime($values['valid_to'])); ?>"/>
                        	<label id = 'error_valid_to<?=$i?>' for="valid_to<?=$i?>" class = "error" style="display:none;">please choose date</label>
                        </td>                                                
	                    <td><input type="text" name="min_stay<?=$i?>" id = "min_stay<?=$i?>" value = "<?=$values['min_night']?>"/>
 							<label id = 'error_min_stay<?=$i?>' for="min_stay<?=$i?>" class = "error" style="display:none;">Enter number only</label>		                       
                        </td>                        
	                    <td><input type="text" name="discount_offer<?=$i?>" id = "discount_offer<?=$i?>" value="<?=$values['discount_offer']?>" class = "required" />
                        	<label id = 'error_discount_offer<?=$i?>' for="discount_offer<?=$i?>" class = "error" style="display:none;">Enter numbers only</label>
							<?=$text?>
                        </td>                                            	
	                    <td><input type="text" name="bookby<?=$i?>" id = "bookby<?=$i?>" value = "<? if($values['book_by']) echo date('m/d/Y', strtotime($values['book_by']));?>"/>
                        	<label id = 'error_bookby<?=$i?>' for="bookby<?=$i?>" class = "error" style="display:none;">Enter proper</label>
                        </td>                                                
	                    <td>
							<input type="hidden" name="spcl_offer_id<?=$i?>" id = "spcl_offer_id<?=$i?>" value="<?=$values['spcl_offer_id']?>"  />
                            <input type="hidden" name="offer_id<?=$i?>" id = "offer_id<?=$i?>" value="<?=$values['id']?>"  />
                            <input type="button" <?=$disabled_a?> class="mws-button black small mws-i-24 i-check"  onclick = 'checkvalid(<?=$i?>)'>
							<input type="button" <?=$disabled_d?> class="mws-button black small mws-i-24 i-cross" onclick = "dactive(<?=$i?>)" >                    
                        </td>
                  </tr>        

        	<?
				$i++;
				}
		    ?>
			</tbody>
            
    </table>        

       
	


<!-- Special Offers tab ends -->

</div><!-- renta rate wrapper div-->
<!-- step 7 ends -->